package com.qf.springboot.controller;


import com.qf.springboot.async.LikeSleep;
import com.qf.springboot.dto.AirDTO;
import com.qf.springboot.mail.MailTest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import springfox.documentation.annotations.ApiIgnore;

import javax.mail.MessagingException;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.Date;

/**
 * projectName: springboot
 *
 * @author dongshihao
 * Date:2021/9/11   10:02
 * description:
 */
@RestController
@Api(tags = "这是用于springboot 测试 controller")
public class TestController {

    @Value("${project-version:v0.0.0}")
    private String version;



    @GetMapping("/test")
    @ApiOperation(value = "这是方法类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "用户名称",defaultValue = "张三"),
            @ApiImplicitParam(name = "age",value = "用户年龄",defaultValue = "23"),
            @ApiImplicitParam(name = "birthday",value = "用户生日",defaultValue = "1999-9-1")
    }
    )
    public String test(String name, Integer age, @DateTimeFormat(pattern = "yyyy-MM-dd")Date birthday,@ApiIgnore HttpSession session){
        return "董世豪"+version;
    }


    @PostMapping("/air/add")
    public AirDTO add(@RequestBody  @Valid AirDTO airDTO, @ApiIgnore  BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            //有参数不满足要求
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            System.out.println("defaultMessage = " + defaultMessage);
            return  null;

        }
        System.out.println("air = " + airDTO);
        return airDTO;
    }

    @Autowired
    private RestTemplate restTemplate;

    //
    @Autowired
    private LikeSleep likeSleep;

    @GetMapping("/rest")
    public String rest()  {

        //POST  http://192.168.100.11/user/signin
        //body {"username" : "xxx","password":"xxx"}

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_PROBLEM_JSON_UTF8);

        String body = "{\"username\" : \"xxx\",\"password\":\"xxx\"}";

        HttpEntity entity = new HttpEntity(body,httpHeaders);


        String  result =  restTemplate.postForObject("http://192.168.100.11/user/signin?", entity, String.class);

        System.out.println("result = " + result);

        System.out.println(Thread.currentThread().getName());
        likeSleep.sleep();
        return result;

    }



    @Autowired
    private MailTest mailTest;

    @GetMapping("/mail")
    public String setMailTest() throws MessagingException {
        mailTest.mailSend();
        return "发送成功";

    }



}
